home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / bin / select-screen-profile < prev    next >
Encoding:
Text File  |  2009-04-08  |  4.1 KB  |  179 lines

  1. #!/bin/sh -e
  2. #
  3. #    select-screen-profile
  4. #    Copyright (C) 2008 Canonical Ltd.
  5. #
  6. #    Authors: Dustin Kirkland <kirkland@canonical.com>
  7. #             Nick Barcet <nick.barcet@ubuntu.com>
  8. #
  9. #    This program is free software: you can redistribute it and/or modify
  10. #    it under the terms of the GNU General Public License as published by
  11. #    the Free Software Foundation, version 3 of the License.
  12. #
  13. #    This program is distributed in the hope that it will be useful,
  14. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #    GNU General Public License for more details.
  17. #
  18. #    You should have received a copy of the GNU General Public License
  19. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  
  21.  
  22. # If you change any strings, please generate localization information with:
  23. #    ./debian/rules get-po
  24.  
  25.  
  26. TEXTDOMAIN="screen-profiles"
  27.  
  28. usage () {
  29.     cat <<EOT
  30. usage: select-screen-profile [(-l|--list)][(-h|--help)][(-s|--set) PROFILE]
  31.     -l,--list           list available profiles
  32.     -s,--set PROFILE    set profile
  33.     -h,--help           this help
  34.  
  35.     Without any parameters, runs interactively.
  36. EOT
  37. }
  38.  
  39. # Initialize variables
  40. BASE_DIR="/usr/share/screen-profiles"
  41. PROFILE_DIR="$BASE_DIR/profiles"
  42. profile=""
  43. selected=-1
  44.  
  45. assert_symlink() {
  46.     if [ -e "$1" ]; then
  47.         if [ ! -L "$1" ]; then
  48.             echo `gettext 'Error:'` $1 `gettext ' file exists, but is not a symlink'`
  49.             exit 1
  50.         fi
  51.     fi
  52. }
  53.  
  54. # If these files exist, they must be a symlink
  55. assert_symlink "$HOME/.screen-profiles/profile"
  56.  
  57. listprofiles() {
  58.     # Display list of profiles, one per line
  59.     # Start with basic profiles
  60.     basename $(ls $PROFILE_DIR/plain 2>/dev/null) 2>/dev/null
  61.     basename $(ls $PROFILE_DIR/*-light 2>/dev/null) 2>/dev/null
  62.     basename $(ls $PROFILE_DIR/*-dark 2>/dev/null) 2>/dev/null
  63.     basename $(ls $PROFILE_DIR/*-black 2>/dev/null) 2>/dev/null
  64.     # Now, list advanced profiles
  65.     for x in $(ls $PROFILE_DIR/*-*_* 2>/dev/null); do
  66.         x=$(basename "$x")
  67.         if [ $x = "common" -o $x = "misc" ]; then
  68.             # Skip the common profile, no value there
  69.             continue
  70.         fi
  71.         if [ $x = "ubuntu" -a -r "$PROFILE_DIR/ubuntu-light" ]; then
  72.             continue
  73.         fi
  74.         echo "$x"
  75.     done
  76. }
  77.  
  78. prompt() {
  79.     # Prompt the user to choose among the available profiles
  80.     echo
  81.     echo `gettext "Select a screen profile: "`
  82.     i=0
  83.     profiles=$(listprofiles)
  84.     for x in $profiles; do
  85.         i=$(expr $i + 1)
  86.         desc="          "
  87.         if [ "$x" = "ubuntu" ]; then
  88.             desc="-light\t"
  89.         elif [ "$x" = "plain" ]; then
  90.             simple=$i
  91.         fi
  92.         [ $i -lt 10 ] && i=" $i"
  93.         echo " $i. $x$desc"
  94.     done
  95.     echo
  96.     selected=x
  97.     count=1
  98.     while /bin/true; do
  99.         if [ $count -gt 5 ]; then
  100.             echo `gettext "ERROR: Invalid selection"`
  101.             exit 1
  102.         fi
  103.         count=`expr $count + 1`
  104.         if [ -z "$selected" -a ! -z "$simple" ]; then
  105.             selected="$simple"
  106.         elif ! test $selected -gt 0 2>/dev/null; then
  107.             read -p "`gettext 'Choose'` 1-$i [$simple]: " -r selected
  108.         elif ! test $selected -le $i 2>/dev/null; then
  109.             read -p "`gettext 'Choose'` 1-$i [$simple]: " -r selected
  110.         else
  111.             break
  112.         fi
  113.     done
  114.     SELECTED="$selected"
  115. }
  116.  
  117. setprofile() {
  118.     # Apply a profile by name or index
  119.     if [ -n "$1" ]; then
  120.         selected="$1"
  121.     else
  122.         selected="$SELECTED"
  123.     fi
  124.     i=0
  125.     found=0
  126.     profiles=$(listprofiles)
  127.     for x in $profiles; do
  128.         i=`expr $i + 1`
  129.         if [ "$i" = "$selected" -o "$x" = "$selected" ]; then
  130.             rm -f "$HOME/.screen-profiles/profile"
  131.             ln -s "$PROFILE_DIR/$x" "$HOME/.screen-profiles/profile"
  132.             found=1
  133.             echo
  134.             if [ "$TERM" = "screen" ]; then
  135.                 echo `gettext 'If you are using the default set of keybindings, press\n<F5><enter> to activate these changes.\n\nOtherwise, exit this screen session and start a new one.'`
  136.             else
  137.                 echo `gettext 'Run "screen" to activate'`
  138.             fi
  139.             echo
  140.             break
  141.         fi
  142.     done
  143.     if [ $found -eq 0 ]; then
  144.         echo "Invalid profile"
  145.     fi
  146. }
  147.  
  148. if [ $# -eq 0 ]; then
  149.     prompt
  150.     setprofile
  151. else
  152.     TEMP=`getopt -o lhs: --long list,help,set: -- "$@"`
  153.     eval set -- "$TEMP"
  154.     while true; do
  155.         case "$1" in
  156.             -s|--set)
  157.                 setprofile "$2"
  158.                 shift 2
  159.                 break
  160.             ;;
  161.             -l|--list)
  162.                 listprofiles
  163.                 shift
  164.                 break
  165.             ;;
  166.             *)
  167.                 usage
  168.                 exit 1
  169.             ;;
  170.             --)
  171.                 shift
  172.                 break
  173.             ;;
  174.         esac
  175.     done
  176. fi
  177.  
  178. exit 0
  179.